
#include "types.h"
#include "characters.h"

/* GP2X button mapping */
enum MAP_KEY
{
	VK_UP         , // 0
	VK_UP_LEFT    , // 1
	VK_LEFT       , // 2
	VK_DOWN_LEFT  , // 3
	VK_DOWN       , // 4
	VK_DOWN_RIGHT , // 5
	VK_RIGHT      , // 6
	VK_UP_RIGHT   , // 7
	VK_START      , // 8
	VK_SELECT     , // 9
	VK_FL         , // 10
	VK_FR         , // 11
	VK_FA         , // 12
	VK_FB         , // 13
	VK_FX         , // 14
	VK_FY         , // 15
	VK_VOL_UP     , // 16
	VK_VOL_DOWN   , // 17
	VK_TAT          // 18
};

const int WINDOW_WIDTH = 320;
const int WINDOW_HEIGHT = 240;
const char* WINDOW_TITLE = "SDL Library Test";
SDL_Surface* screen;
SDL_Surface* sprite;
SDL_Surface* small_font;
SDL_Surface* large_font;
SDL_Joystick *joy = NULL;
Mix_Chunk *sound = NULL;
Mix_Music *music = NULL;

int done=0;
int continue_running=0;

int gUp;
int gDown;
int gLeft;
int gRight;
int gButtonX;
int gButtonY;
int gButtonB;
int gButtonA;
int gButtonL;
int gButtonR;

        
void drawText(SDL_Surface* font, SDL_Surface* screen, char* a, int x, int y);
void drawCharacter(SDL_Surface* font, SDL_Surface* screenSurface, int srcX, int srcY, int dstX, int dstY, int width, int height);

void Shutdown()
{	
	SDL_FreeSurface(screen);																// Release the memory allocated to screen
	SDL_FreeSurface(sprite);
    SDL_FreeSurface(small_font);																// Release the memory allocated to sprite
	SDL_FreeSurface(large_font);
    Mix_FreeChunk(sound);																		// Release the memory allocated to sound
	Mix_FreeMusic(music);																		// Release the memory allocated to music
	Mix_CloseAudio();																				// Close SDL_Mixer Audio																							// Close SDL_TTF
																							// if GP2X isnt defined (i.e. PC) close SDL 
	SDL_Quit();

	#ifdef GP2X
	chdir("/usr/gp2x");
	execl("/usr/gp2x/gp2xmenu", "/usr/gp2x/gp2xmenu", NULL);
    #endif
}

int SDLMixerMOD(char* filename)
{
	//SDL_Mixer MOD play test
	music = Mix_LoadMUS(filename);
	Mix_PlayMusic(music, 0);
	return 1;
}

void drawText_variable(SDL_Surface* font, SDL_Surface* screen, char* a, int x, int y)
{
     int j,width,height,srcX,DestX,DestY;
     
     j=0;
     DestX=x;
     DestY=y;
     
     if(font==small_font) height = 8; else height = 12;
          
     while(j!=strlen(a))
     {
        switch(a[j])
        {
        case ' ': if(font==small_font) width = 4; else width = 0; break;
        case 'A': if(font==small_font) width = 8; else width = 0; break;
        case 'B': if(font==small_font) width = 6; else width = 0; break;
        case 'C': if(font==small_font) width = 6; else width = 0; break;
        case 'D': if(font==small_font) width = 6; else width = 0; break;
        case 'E': if(font==small_font) width = 6; else width = 0; break;
        case 'F': if(font==small_font) width = 6; else width = 0; break;
        case 'G': if(font==small_font) width = 5; else width = 0; break;
        case 'H': if(font==small_font) width = 7; else width = 0; break;
        case 'I': if(font==small_font) width = 6; else width = 0; break;
        case 'J': if(font==small_font) width = 6; else width = 0; break;
        case 'K': if(font==small_font) width = 6; else width = 0; break;
        case 'L': if(font==small_font) width = 6; else width = 0; break;
        case 'M': if(font==small_font) width = 6; else width = 0; break;
        case 'N': if(font==small_font) width = 5; else width = 0; break;
        case 'O': if(font==small_font) width = 8; else width = 0; break;
        case 'P': if(font==small_font) width = 6; else width = 0; break;
        case 'Q': if(font==small_font) width = 7; else width = 0; break;
        case 'R': if(font==small_font) width = 6; else width = 0; break;
        case 'S': if(font==small_font) width = 6; else width = 0; break;
        case 'T': if(font==small_font) width = 6; else width = 0; break;
        case 'U': if(font==small_font) width = 6; else width = 0; break;
        case 'V': if(font==small_font) width = 6; else width = 0; break;
        case 'W': if(font==small_font) width = 8; else width = 0; break;
        case 'X': if(font==small_font) width = 7; else width = 0; break;
        case 'Y': if(font==small_font) width = 8; else width = 0; break;
        case 'Z': if(font==small_font) width = 7; else width = 0; break;
        
        case 'a': if(font==small_font) width = 7; else width = 0; break;
        case 'b': if(font==small_font) width = 6; else width = 0; break;
        case 'c': if(font==small_font) width = 6; else width = 0; break;
        case 'd': if(font==small_font) width = 6; else width = 0; break;
        case 'e': if(font==small_font) width = 6; else width = 0; break;
        case 'f': if(font==small_font) width = 5; else width = 0; break;
        case 'g': if(font==small_font) width = 6; else width = 0; break;
        case 'h': if(font==small_font) width = 6; else width = 0; break;
        case 'i': if(font==small_font) width = 6; else width = 0; break;
        case 'j': if(font==small_font) width = 4; else width = 0; break;
        case 'k': if(font==small_font) width = 7; else width = 0; break;
        case 'l': if(font==small_font) width = 7; else width = 0; break;
        case 'm': if(font==small_font) width = 7; else width = 0; break;
        case 'n': if(font==small_font) width = 7; else width = 0; break;
        case 'o': if(font==small_font) width = 7; else width = 0; break;
        case 'p': if(font==small_font) width = 7; else width = 0; break;
        case 'q': if(font==small_font) width = 7; else width = 0; break;
        case 'r': if(font==small_font) width = 7; else width = 0; break;
        case 's': if(font==small_font) width = 7; else width = 0; break;
        case 't': if(font==small_font) width = 7; else width = 0; break;
        case 'u': if(font==small_font) width = 7; else width = 0; break;
        case 'v': if(font==small_font) width = 7; else width = 0; break;
        case 'w': if(font==small_font) width = 7; else width = 0; break;
        case 'x': if(font==small_font) width = 7; else width = 0; break;
        case 'y': if(font==small_font) width = 7; else width = 0; break;
        case 'z': if(font==small_font) width = 7; else width = 0; break;
        
        default: width = 0; break;
        }
        
        srcX = 256 + 8*(a[j]-32);
        
        if(a[j]=='\n') 
        { 
          j++;
          DestY+=height + 1;
          DestX=x + 1;
        } else {
          j++;
          //DestX+=8;
          DestX += width;
          drawCharacter(font, screen, srcX, 0, DestX, DestY, 8, 8);
        }
     } 
}

void drawText(SDL_Surface* font, SDL_Surface* screen, char* a, int x, int y)
{
     int j,height,width,srcX,DestX,DestY;
     
     j=0;
     DestX=x;
     DestY=y;
     
     if(font==small_font) {                 
     height = 8; 
     width = 8;
     } else { 
     height = 16;
     width = 8;
     }
          
     while(j!=strlen(a))
     {
        srcX = 256 + 8*(a[j]-32);
        
        if(a[j]=='\n') 
        { 
          j++;
          DestY+=height;
          DestX=x;
        } else {
          j++;
          DestX+=width;
          drawCharacter(font, screen, srcX, 0, DestX, DestY, 8, height);
        }
     } 
     SDL_Flip(screen);
}

void drawTextInt(SDL_Surface* font, SDL_Surface* screen, int number, int x, int y)
{
	char tmp_num[16];
	sprintf((char*)tmp_num, "%d", number);
    drawText(font, screen, tmp_num, x, y);
}

#define TEXTBOX_LOADED     0
#define LOAD_TEXTBOX       1

void drawTextBox(int player_no, char* text, int load_textbox)
{
    char player_name[256];
    
    continue_running = 0;
    
    if(load_textbox){
    SDL_Surface *textbox;
    SDL_Rect dstRect;
	dstRect.x = 0;
	dstRect.y = 162;
	dstRect.w = 320;
	dstRect.h = 80;
	
	textbox = SDL_LoadBMP("images/textbox.bmp");
	SDL_BlitSurface(textbox, NULL, screen, &dstRect);
	SDL_Flip(screen);
    }
	
	SDL_Rect dstClearRect;
	dstClearRect.x = 75;
	dstClearRect.y = 178;
	dstClearRect.w = 216;
	dstClearRect.h = 52;
	
	SDL_FillRect(screen, &dstClearRect, SDL_MapRGB(screen->format, 0,0,0));

	sprintf(player_name, "%s : \n%s", player_info[player_no].npcName,text);
	
	text = NULL;
	
	drawText(large_font, screen, player_name, 69, 178);
}

void drawCharacter(SDL_Surface* font, SDL_Surface* screenSurface, int srcX, int srcY, int dstX, int dstY, int width, int height)
{
	SDL_Rect srcRect;
	srcRect.x = srcX;
	srcRect.y = srcY;
	srcRect.w = width;
	srcRect.h = height;

	SDL_Rect dstRect;
	dstRect.x = dstX;
	dstRect.y = dstY;
	dstRect.w = width;
	dstRect.h = height;

	SDL_BlitSurface(font, &srcRect, screenSurface, &dstRect);
}

void drawSprite(SDL_Surface* imageSurface, SDL_Surface* screenSurface, int srcX, int srcY, int dstX, int dstY, int width, int height)
{
	SDL_Rect srcRect;
	srcRect.x = srcX;
	srcRect.y = srcY;
	srcRect.w = width;
	srcRect.h = height;

	SDL_Rect dstRect;
	dstRect.x = dstX;
	dstRect.y = dstY;
	dstRect.w = width;
	dstRect.h = height;

	SDL_BlitSurface(imageSurface, &srcRect, screenSurface, &dstRect);
}

int TestInput()
{

	// SDL Input Test
	int endtest = 0;
	char g_string[255];
	strcpy(g_string," ");

	printf("TestInput function started.\n");									// debug output to serial cable example
	while (!endtest)
	{
		//SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0,0,0));
		drawText(small_font, screen, "Input Test", 0, 0);
		drawText(small_font, screen, g_string, 0, 30);
		drawText(small_font, screen, "Correct_answer = ", 0, 50);
		drawText(small_font, screen, "Press START to exit", 0, 215);
		SDL_Flip(screen);

		SDL_Event event;

		while( SDL_PollEvent( &event ) )
		{
			switch( event.type )
			{
				case SDL_JOYBUTTONDOWN:
					switch( event.jbutton.button )
					{
						case VK_TAT:
							strcpy(g_string,"Joystick BUTTON");
							break;
						case VK_START:
							{strcpy(g_string,"Button START");endtest=1;}
							break;
						case VK_SELECT:
							strcpy(g_string,"Button SELECT");
							break;
						case VK_VOL_UP:
							strcpy(g_string,"Volume UP");
							break;
						case VK_VOL_DOWN:
							strcpy(g_string,"Volume DOWN");
							break;
						case VK_LEFT :
							strcpy(g_string,"Joystick LEFT");
							break;
						case VK_RIGHT :
							strcpy(g_string,"Joystick RIGHT");
							break;
						case VK_UP :
							strcpy(g_string,"Joystick UP");
							break;
						case VK_DOWN :
							strcpy(g_string,"Joystick DOWN");
							break;
						case VK_FY:
							strcpy(g_string,"Button Y");
							break;
						case VK_FB:
							strcpy(g_string,"Button B");
							break;
						case VK_FX :
							strcpy(g_string,"Button X");
							break;
						case VK_FA :
							strcpy(g_string,"Button A");
							break;
						case VK_FL :
							strcpy(g_string,"Button L");
							break;
						case VK_FR :
							strcpy(g_string,"Button R");
							break;
						default:
							break;
					}

				case SDL_KEYDOWN:
					switch( event.key.keysym.sym )
					{
						case SDLK_f:
							strcpy(g_string,"Joystick BUTTON");
							break;
						case SDLK_LEFT:
							strcpy(g_string,"Joystick LEFT");
							break;
						case SDLK_RIGHT:
							strcpy(g_string,"Joystick RIGHT");
							break;
						case SDLK_UP:
							strcpy(g_string,"Joystick UP");
							break;
						case SDLK_DOWN:
							strcpy(g_string,"Joystick DOWN");
							break;
						case SDLK_w:
							strcpy(g_string,"Button Y");
							endtest=1;
							break;
						case SDLK_d:
							strcpy(g_string,"Button B");
							break;
						case SDLK_s:
							strcpy(g_string,"Button X");
							break;
						case SDLK_a:
							strcpy(g_string,"Button A");
							break;
						case SDLK_q:
							strcpy(g_string,"Button L");
							break;
						case SDLK_e:
							strcpy(g_string,"Button R");
							break;
						case SDLK_x:
							{strcpy(g_string,"Button START");endtest=1;}
							break;
						case SDLK_z:
							strcpy(g_string,"Button SELECT");
							break;
						case SDLK_c:
							strcpy(g_string,"Volume DOWN");
							break;
						case SDLK_v:
							strcpy(g_string,"Volume UP");
							break;
						default:
							break;
					}
			}
		}
	}
	printf("TestInput function ended.\n");											// debug output to serial cable example
return 1;
}


/*int WaitforCorrectAnswer()
{
    int quit=0;
    while(!quit)
	{
		SDL_Event event;

		while( SDL_PollEvent( &event ) )
		{
			switch( event.type )
			{
				case SDL_JOYBUTTONDOWN:														// GP2X buttons

					switch( event.jbutton.button )
					{
						case VK_FA :
							if(answer[qinfo.questionNum]==ANS_A) 
							correct_answer=1; else correct_answer=0;
							
							quit=1;
							//AnswerCorrect();
						    //else
							//AnswerWrong();
							break;
						case VK_FX :
							if(answer[qinfo.questionNum]==ANS_X)
							correct_answer=1; else correct_answer=0;
							
							quit=1;
							//AnswerCorrect();
							//else
							//AnswerWrong();
							break;
						case VK_FB :
							if(answer[qinfo.questionNum]==ANS_B)
							correct_answer=1; else correct_answer=0;
							
							quit=1;
							//AnswerCorrect();
							//else
							//AnswerWrong();
							break;
						case VK_FY :
							if(answer[qinfo.questionNum]==ANS_Y)
							correct_answer=1; else correct_answer=0;
							
							quit=1;
							//AnswerCorrect();
							//else
							//AnswerWrong();
							break;
						case VK_START :
							Shutdown();
							break;
						default:
							quit=1;
							break;
					}

				case SDL_KEYDOWN:																	// PC buttons

					switch( event.key.keysym.sym )
					{
						case SDLK_ESCAPE:
							Shutdown();
							break;
						case SDL_QUIT:
							Shutdown();
							break;						
						case SDLK_a:
							if(answer[qinfo.questionNum]==ANS_A)
							correct_answer=1; else correct_answer=0;
							
							quit=1;
							break;
						case SDLK_s:
							if(answer[qinfo.questionNum]==ANS_X)
							correct_answer=1; else correct_answer=0;
							
							quit=1;
							break;
						case SDLK_d:
							if(answer[qinfo.questionNum]==ANS_B)
							correct_answer=1; else correct_answer=0;
							
							quit=1;
							break;
						case SDLK_w:
							if(answer[qinfo.questionNum]==ANS_Y)
							correct_answer=1; else correct_answer=0;
							
							quit=1;
							break;
						default:
							quit=1;
							break;
					}
			}
		}
	}
	return 1;
}
     
void WaitForKey()
{
    int quit=0;
    int right=0;
    char temp[256];
	while(!quit)
	{
		SDL_Event event;
		drawText(screen, temp, 0, 30);

		while( SDL_PollEvent( &event ) )
		{
			switch( event.type )
			{
				case SDL_JOYBUTTONDOWN:														// GP2X buttons

					switch( event.jbutton.button )
					{
						case VK_FA :
							if(answer[qinfo.questionNum]==ANS_A) 
							right=1; else right=0;
							
							quit=1;
							//AnswerCorrect();
						    //else
							//AnswerWrong();
							break;
						case VK_FX :
							if(answer[qinfo.questionNum]==ANS_X)
							right=1; else right=0;
							
							quit=1;
							//AnswerCorrect();
							//else
							//AnswerWrong();
							break;
						case VK_FB :
							if(answer[qinfo.questionNum]==ANS_B)
							right=1; else right=0;
							
							quit=1;
							//AnswerCorrect();
							//else
							//AnswerWrong();
							break;
						case VK_FY :
							if(answer[qinfo.questionNum]==ANS_Y)
							right=1; else right=0;
							
							quit=1;
							//AnswerCorrect();
							//else
							//AnswerWrong();
							break;
						case VK_START :
							Shutdown();
							break;
						default:
							quit=1;
							break;
					}

				case SDL_KEYDOWN:																	// PC buttons

					switch( event.key.keysym.sym )
					{
						case SDLK_ESCAPE:
							Shutdown();
							break;
						case SDL_QUIT:
							Shutdown();
							break;						
						case SDLK_a:
							if(answer[qinfo.questionNum]==ANS_A)
							right=1; else right=0;
							
							quit=1;
							break;
						case SDLK_s:
							if(answer[qinfo.questionNum]==ANS_X)
							right=1; else right=0;
							
							quit=1;
							break;
						case SDLK_d:
							if(answer[qinfo.questionNum]==ANS_B)
							right=1; else right=0;
							
							quit=1;
							break;
						case SDLK_w:
                            strcpy(temp,"Button Y");
                             
							if(answer[qinfo.questionNum]==ANS_Y)
							right=1; else right=0;
							
							quit=1;
							break;
						default:
							quit=1;
							break;
					}
			}
		}
	}
	
	screen = SDL_LoadBMP("images/answer.bmp");
     
	if(right)
    {
     drawText(screen, "You got it right \nWell done!", 20, 60);
     SDL_Flip(screen);
     qinfo.questionNum++;
    } else {
     sprintf(temp,"Sorry the correct answer is: \n%s", answers[qinfo.questionNum][answer[qinfo.questionNum]]);
     drawText(screen, temp, 20, 60);
     SDL_Flip(screen);
     qinfo.questionNum++;
    }
	
	Testquiz();
}*/

#ifdef WIN32
int sleep(int t)
{
  int i;
  for (i = 0; i < t*100000000; i++);
  return 1;
}
#endif


void CheckKeys()
{
     while(!continue_running)
     {
        SDL_Event event;
		//drawText(screen, temp, 0, 30);

		while( SDL_PollEvent( &event ) )
		{
			switch( event.type )
			{
				case SDL_JOYBUTTONDOWN:														// GP2X buttons

					switch( event.jbutton.button )
					{
						case VK_FA :
							break;
						case VK_FX :
                            continue_running = 1;
							break;
						case VK_FB :
							break;
						case VK_FY :
							break;
						case VK_START :
							Shutdown();
							break;
						default:
							continue_running=0;
							break;
					}
					break;

				case SDL_KEYDOWN:																	// PC buttons

					switch( event.key.keysym.sym )
					{
						case SDLK_ESCAPE:
							Shutdown();
							break;
						case SDL_QUIT:
							Shutdown();
							break;						
						case SDLK_a:
							break;
						case SDLK_s:
                            continue_running = 1;
							break;
						case SDLK_d:
							break;
						case SDLK_w:
							break;
						default:
							continue_running=0;
							break;
					}
					break;
			}
		}
     }
}
